import request from '@/utils/request'
import * as mock from './mock/cash'

const isMock = import.meta.env.VITE_API_MOCK === 'true'

/**
 * 获取币种流水记录列表
 * @param {Object} params 查询参数
 * @param {string} [params.currency] - 币种
 * @param {string} [params.type] - 类型 (income/expense)
 * @param {string} [params.status] - 状态 (completed/pending/failed)
 * @param {string} [params.startDate] - 开始日期
 * @param {string} [params.endDate] - 结束日期
 * @param {number} [params.page=1] - 页码
 * @param {number} [params.pageSize=10] - 每页条数
 * @returns {Promise}
 */
export function getRecordList(params) {
  if (isMock) {
    return Promise.resolve(mock.getRecordList(params))
  }
  return request({
    url: '/user/flows',
    method: 'get',
    params
  })
}

/**
 * 获取币种流水统计数据
 * @param {Object} params 查询参数
 * @param {string} [params.currency] - 币种
 * @param {string} [params.period] - 周期 (today/week/month/lastMonth/year)
 * @returns {Promise}
 */
export function getRecordStatistics(params) {
  if (isMock) {
    return Promise.resolve(mock.getRecordStatistics(params))
  }
  return request({
    url: '/user/flowCount',
    method: 'get',
    params
  })
}

/**
 * 获取币种流水详情
 * @param {string} id 流水记录ID
 * @returns {Promise}
 */
export function getRecordDetail(id) {
  if (isMock) {
    return Promise.resolve(mock.getRecordDetail(id))
  }
  return request({
    url: `/cash/record/${id}`,
    method: 'get'
  })
}

/**
 * 获取交易详情
 * @param {string} id 交易ID
 * @returns {Promise}
 */
export function getTransactionDetail(id) {
  if (isMock) {
    return Promise.resolve(mock.getTransactionDetail(id))
  }
  return request({
    url: `/cash/transaction/${id}`,
    method: 'get'
  })
}

/**
 * 获取交易记录列表
 * @param {Object} params 查询参数
 * @param {string} [params.currency] - 币种
 * @param {string} [params.type] - 类型 (deposit/withdraw/transfer/interest)
 * @param {string} [params.status] - 状态 (completed/pending/failed)
 * @param {string} [params.startDate] - 开始日期
 * @param {string} [params.endDate] - 结束日期
 * @param {number} [params.page=1] - 页码
 * @param {number} [params.pageSize=10] - 每页条数
 * @returns {Promise}
 */
export function getTransactions(params) {
  if (isMock) {
    return Promise.resolve(mock.getTransactions(params))
  }
  return request({
    url: '/user/transList',
    method: 'get',
    params
  })
}

/**
 * 获取支持的充值币种列表
 * @returns {Promise}
 */
export function getSupportedCurrencies(param) {
  if (isMock) {
    return Promise.resolve(mock.getSupportedCurrencies())
  }
  return request({
    url: '/cfg/icons',
    method: 'get',
    param
  })
}


/**
 * 用户充值
 * @param {Object} data 查询参数
 * @param {string} data.currency - 币种
 * @param {string} [data.network] - 网络类型
 * @returns {Promise}
 */
export function userDeposit(data) {
  if (isMock) {
    return Promise.resolve(mock.getDepositAddress(params))
  }
  return request({
    url: '/user/charge',
    method: 'post',
    data
  })
}


/**
 * 用户充值Hash回填
 * @param {Object} data 查询参数
 * @param {string} data.currency - 币种
 * @param {string} [data.network] - 网络类型
 * @returns {Promise}
 */
export function userDepositHash(data) {
  if (isMock) {
    return Promise.resolve(mock.getDepositAddress(params))
  }
  return request({
    url: '/user/chargeHash',
    method: 'post',
    data
  })
}


/**
 * 提交提现申请
 * @param {Object} data 提现数据
 * @param {string} data.currency - 币种
 * @param {string} data.amount - 金额
 * @param {string} data.address - 提现地址
 * @param {string} data.password - 交易密码
 * @returns {Promise}
 */
export function submitWithdraw(data) {
  if (isMock) {
    return Promise.resolve(mock.submitWithdraw(data))
  }
  return request({
    url: '/user/withdrawal',
    method: 'post',
    data
  })
}
